<?php
namespace app\center\controller;
use app\center\logic\MyCpLogic;

/**
 * 我的社区巡店api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/09/20
 * Time: 11:52
 */
class MyCp extends Base {

    public function __construct(){
        parent::__construct();
        $this->mc_logic = new MyCpLogic();
    }

    /**
     * 获取我的社区巡店
     * @return \think\response\Json
     */
    public function ajax_get_my_cp(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->mc_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = $this->mc_logic->get_search_my_cp_maps($u_detail['openid'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('cp_floor')->get_list($maps,'id,cp_type,cp_branchid,branchid,floorid,start_time,end_time,status','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['cp_type_zw'] = $v->cp_type_name;
            $list[$k]['status_zw'] = $v->status_name;
            $list[$k]['end_time'] = $v['status'] == 1 ? $v['end_time'] : $v['start_time'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$v['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branch_zw'] = $b_detail['name'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$v['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,name');
            $list[$k]['floor_zw'] = $f_detail['name'];
        }

        $data['list'] = $list;
        $data['page'] = $params['page'];
        return json($this->mc_logic->get_response(1,'OK',$data));
    }
}
